﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Motoball.screenmanager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Motoball.screens
{
    public class PauseScreen : OptionsSelectScreen
    {
        Texture2D background;
        Color backgroundColor;
        
        public PauseScreen(string screenName)
            : base(screenName)
        {
            backgroundColor = Color.Black;
            backgroundColor.A = 180;
        }

        #region Methods

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Begin();
            SpriteBatch.Draw(background, new Rectangle(0, 0, screenManager.Game.Window.ClientBounds.Width, screenManager.Game.Window.ClientBounds.Height), null, backgroundColor);
            SpriteBatch.End();
            base.Draw(gameTime);
        }

        public override void LoadContent()
        {
            ContentManager content = screenManager.Content;
            //AddItem(new MenuItem(new BasicModel(content.Load<Model>("Models\\Menu_Resume"))));
            //AddItem(new MenuItem(new BasicModel(content.Load<Model>("Models\\Menu_Restart"))));
            //AddItem(new MenuItem(new BasicModel(content.Load<Model>("Models\\Menu_Exit"))));

            AddItem("Models\\Resume");
            AddItem("Models\\Restart");
            AddItem("Models\\Exit");

            //make a list of all diffColor-entries
            List<string>[] keys = new List<string>[3];
            for (int i = 0; i < 3; i++)
            {
                keys[i] = new List<string>(items[i].model.diffColors.Keys);
            }
            //tint everything medium grey
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < keys[i].Count; j++)
                {
                    items[i].model.diffColors[keys[i][j]] = new Vector3(0.8f);
                }
            }
            //tint specific areas differently
            //Exit
            BasicModel model = items[2].model;
            model.diffColors["Tuer"] = new Vector3(0, 0.7f / 2f, 0);
            model.diffColors["Mann_Kopf"] = Vector3.One * 2f;
            model.diffColors["Mann_Körper"] = Vector3.One * 2f;
            model.diffColors["Pfeil"] = Vector3.One * 2f;

            background = screenManager.Content.Load<Texture2D>("Images\\blank");
            TopItem = 0;

            for (int i = 0; i < 3; i++)
            {
                items[i].center = new Vector3(0,Camera.gameplayCamDistance/1.2f,-20f);
                items[i].model.drawSetId = "foreground";
            }
        }

        public override void Reset()
        {
            base.Reset();
            itemSelected = false;
        }
        bool itemSelected = false;
        protected override bool DoSomething()
        {
            itemSelected = true;
            return true;
        }

        protected override void Exit()
        {
            GameplayScreen gameplay = (GameplayScreen)screenManager.GetScreen("gameplayScreen");
            CountdownScreen countdown = (CountdownScreen)screenManager.GetScreen("countdownScreen");
            //if not item selected resume game
            if (!itemSelected)
                TopItem = 0;
            switch(TopItem)
            {
                //resume to game
                case 0:
                    //countdown is set to active, it's currently NOT really active, so gameplay has to be set to active
                    if (countdown.active)
                        gameplay.active = GameplayScreen.Activity.Active;
                    else
                    {
                        countdown.active = true;
                        gameplay.active = GameplayScreen.Activity.Counting;
                    }
                    gameplay.SetElementsActive(true);
                    screenManager.RemoveScreen(this);
                    break; 
                    //new game
                case 1:
                    screenManager.RemoveScreen(countdown);
                    gameplay.StartGame(true);
                    screenManager.RemoveScreen(this);
                    break;
                //leave to mainMenu
                case 2:
                    //set cam back to menu-view
                    CameraMovementScreen camMovement = (CameraMovementScreen)screenManager.GetScreen("cameraMovementScreen");
                    camMovement.StartCameraMovement(Camera.gameplayView, Camera.menuView);
                    screenManager.AddScreen(camMovement);

                    screenManager.RemoveScreen(gameplay);
                    screenManager.RemoveScreen(gameplay);
                    screenManager.RemoveScreen(countdown);
                    base.Exit();
                    break;
            }               
        }
        #endregion
    }
}
